package com.itzhihao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itzhihao.common.constant.ExistStatus;
import com.itzhihao.entity.Address;
import com.itzhihao.entity.Cart;
import com.itzhihao.entity.Store;
import com.itzhihao.entity.vo.CartVo;
import com.itzhihao.mapper.CartMapper;
import com.itzhihao.service.ICartService;
import com.itzhihao.service.IStoreService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart>
    implements ICartService {

    @Resource
    private CartMapper cartMapper;
    @Resource
    private IStoreService storeService;

    @Override
    public Cart getDetail(Long id) {
        LambdaQueryWrapper<Cart> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getIsDeleted, ExistStatus.EXIST_STATUS.ordinal())
                .eq(Cart::getId,id);
        return cartMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean updateOrSave(Cart cart) {
        Long userId = SecurityUtils.getUserId();
        cart.setUserId(userId);
        cart.setUpdateTime(DateUtils.getNowDate());
        cart.setUpdateBy(userId.toString());
        int i = 0;
        // 修改
        if(cart.getId() != null){
            i = cartMapper.updateById(cart);
        }else{
            cart.setCreateBy(userId.toString());
            cart.setCreateTime(DateUtils.getNowDate());
            cart.setIsDeleted(ExistStatus.EXIST_STATUS.ordinal());
            cart.setUpdateTime(DateUtils.getNowDate());
            i = cartMapper.insert(cart);
        }
        if(i > 0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public TableDataInfo getList(PageDomain pageDomain) {

        Long userId = SecurityUtils.getUserId();
        pageDomain.setPageStart((pageDomain.getPageNum()-1)*pageDomain.getPageSize());
        List<CartVo> list = cartMapper.selectCartList(pageDomain,userId);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询到"+list.size()+"条数据");
        tableDataInfo.setRows(list);
        return tableDataInfo;
    }

    @Override
    public boolean deleteById(Long id) {
        LambdaUpdateWrapper<Cart> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Cart::getIsDeleted, ExistStatus.NO_EXIST_STATUS.ordinal());
        updateWrapper.eq(Cart::getId,id);
        int i = cartMapper.update(null, updateWrapper);
        if(i > 0){
            return true;
        }else{
            return false;
        }
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteByIds(List<Long> idLongList) {
        for (Long id:idLongList
        ) {
            boolean b = deleteById(id);
            if(!b){
                return false;
            }
        }
        return true;
    }
}
